package api

import (
	"context"
	"fmt"

	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/managers/robotmanager"
	"dev.ixmxm.top/rms/utils/log"
	"dev.ixmxm.top/rms/versions"
)

// @Summary 获取所有RMS系统配置数据
// @Description
// @Accept application/json
// @Produce application/json
// @Success 0 {object} ResponseExampleStruct "返回数据格式"
// @Success 1 {object} model.SystemConfig "data数据：机器人上线配置"
// @Tags 系统
// @Router /system/get_system_config [get]
func (hg *HttpGateway) GetSystemConfig(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	return hg.dispatcher.SystemConfig, nil
}

// @Summary 系统配置数据保存
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfigParams true "系统配置数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/save_system_config [post]
func (hg *HttpGateway) SaveSystemConfig(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfigParams)
	cfg := *hg.dispatcher.SystemConfig
	cfg.LogCfg = p.LogCfg
	cfg.MiddlewareCfg = p.MiddlewareCfg
	cfg.RobotOnlineCfg = p.RobotOnlineCfg
	cfg.TrafficCfg = p.TrafficCfg
	defer hg.dispatcher.SaveSystemConfigToCache(&cfg)
	return nil, robotmanager.AddConfigToDB(ctx, &cfg)
}

// @Summary 系统配置(日志)数据保存
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_LogParams true "系统配置(日志)数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/save_system_config_log [post]
func (hg *HttpGateway) SaveSystemConfig_Log(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_LogParams)
	cfg := *hg.dispatcher.SystemConfig
	cfg.LogCfg = p.LogCfg
	defer hg.dispatcher.SaveSystemConfigToCache(&cfg)
	return nil, robotmanager.AddConfigToDB(ctx, &cfg)
}

// @Summary 系统配置(中间件)数据保存
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_MiddlewareParams true "系统配置(中间件)数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/save_system_config_middleware [post]
func (hg *HttpGateway) SaveSystemConfig_Middleware(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_MiddlewareParams)
	cfg := *hg.dispatcher.SystemConfig
	cfg.MiddlewareCfg = p.MiddlewareCfg
	defer hg.dispatcher.SaveSystemConfigToCache(&cfg)
	return nil, robotmanager.AddConfigToDB(ctx, &cfg)
}

// @Summary 系统配置(小车上线)数据保存
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_OnlineParams true "系统配置(小车上线)数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/save_system_config_online [post]
func (hg *HttpGateway) SaveSystemConfig_Online(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_OnlineParams)
	cfg := *hg.dispatcher.SystemConfig
	cfg.RobotOnlineCfg = p.RobotOnlineCfg
	defer hg.dispatcher.SaveSystemConfigToCache(&cfg)
	return nil, robotmanager.AddConfigToDB(ctx, &cfg)
}

// @Summary 系统配置(交管策略)数据保存
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_TrafficParams true "系统配置(交管策略)数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/save_system_config_traffic [post]
func (hg *HttpGateway) SaveSystemConfig_Traffic(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_TrafficParams)
	cfg := *hg.dispatcher.SystemConfig
	cfg.TrafficCfg = p.TrafficCfg
	defer hg.dispatcher.SaveSystemConfigToCache(&cfg)
	return nil, robotmanager.AddConfigToDB(ctx, &cfg)
}

// @Summary 获取并打印所有错误码
// @Description
// @Accept application/json
// @Produce application/json
// @Success 0 {object} ResponseExampleList "返回数据格式"
// @Success 1 {object} []Err "data数据：错误码集合"
// @Tags 系统
// @Router /system/get_error_codes [get]
func (hg *HttpGateway) GetErrorCodes(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	errs := make([]Err, 0)
	for _, err := range codes.AllCodes {
		str := fmt.Sprintf("0x%x;%d;%s", err.GetCode(), err.GetCode(), err.Error())
		errs = append(errs, Err{Code: err.GetCode(), CodeHex: fmt.Sprintf("0x%x", err.GetCode()), Msg: err.Error(), ToString: str})
		fmt.Printf("0x%x\t|\t%d\t|\t%s\n", err.GetCode(), err.GetCode(), err.Error())
	}
	return errs, nil
}

// @Summary 获取版本信息
// @Description
// @Accept application/json
// @Produce application/json
// @Success 0 {object} ResponseExampleList "返回数据格式"
// @Success 1 {object} []versions.Version "data数据：错误码集合（第0个是当前版本）"
// @Tags 系统
// @Router /system/get_versions [get]
func (hg *HttpGateway) GetVersions(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	return versions.GetVersions(), nil
}

//------定时充电相关-------------------

// @Summary 添加一个定时充电规则
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_TimerChargerParams true "新增定时充电规则"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/add_one_timer_charger_rule [post]
func (hg *HttpGateway) AddOneTimerChargerRule(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_TimerChargerParams)
	//验证数据是否正常
	err := robotmanager.AddOneTimerChargerRule(ctx, p.DayChargerRule)
	if err != nil {
		log.ErrorfWithContext(ctx, "add one timer charger rule error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 修改一个定时充电规则
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveSystemConfig_TimerChargerParams true "更新的用户数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/update_one_timer_charger_rule [post]
func (hg *HttpGateway) UpdateOneTimerChargerRule(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_TimerChargerParams)
	//验证数据是否正常
	err := robotmanager.UpdateTimerChargerRule(ctx, p.DayChargerRule)
	if err != nil {
		log.ErrorfWithContext(ctx, "update one timer charger rule error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 删除一个定时充电规则
// @Description
// @Accept application/json
// @Produce application/json
// @Param id query uint16 true "规则编号"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 系统
// @Router /system/delete_one_timer_charger_rule [delete]
func (hg *HttpGateway) DeleteTimerChargerRuleById(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveSystemConfig_RuleId)
	err := robotmanager.DeleteOneTimerCharger(ctx, p.Id)
	if err != nil {
		log.ErrorfWithContext(ctx, "delete one timer charger rule by ruleId error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 获得所有的定时充电规则
// @Description
// @Accept application/json
// @Produce application/json
// @Success 1 {object} []model.DayChargerRule "data数据:用户集合"
// @Tags 系统
// @Router /system/get_all_timer_charger_rule [get]
func (hg *HttpGateway) GetAllTimerChargerRule(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	res, err := robotmanager.GetAllTimerChargerRule(ctx)
	if err != nil {
		log.ErrorfWithContext(ctx, "get all timer charger rule error:%s", err.UnWrapError())
		return nil, err
	}
	return res, nil
}
